package it.appcatalogobase;

import java.util.List;
import java.util.Random;

import it.appcatalogobase.R;


import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.os.Build;

public class CategoryListActivity extends ActionBarActivity {
	private CategoriaDataSource datasourcecat;
	private ProdottoDataSource datasourcepr;
	private ListView l;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_category_list);

//		if (savedInstanceState == null) {
//			getSupportFragmentManager().beginTransaction()
//					.add(R.id.container, new PlaceholderFragment()).comm    it();
//		}
		datasourcecat = new CategoriaDataSource(this);
	    datasourcecat.open();
//	    datasourcecat.deleteCategorie();
//	    datasourcecat.createCategoria("Caffè", "I nostri tipi di caffè");
//	    datasourcepr = new ProdottoDataSource(this);
//	    datasourcepr.open();
	    final List<Categoria> valuesCat = datasourcecat.getAllCategorie();
	    ArrayAdapter<Categoria> adapter = new ArrayAdapter<Categoria>(this,
	            android.R.layout.simple_list_item_1, valuesCat);
	    l = (ListView) findViewById(R.id.listAc);
	    MyAdapter ad = new MyAdapter(this, valuesCat);
	    l.setAdapter(ad);
	    l.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent i = new Intent(CategoryListActivity.this,ListProductsActivity.class);
				i.putExtra("type", "cat");
				i.putExtra("cat", valuesCat.get(arg2).getNome());
				i.putExtra("id", valuesCat.get(arg2).getId());
				startActivity(i);
			}
		});
	}
	
//	public void onClick(View view) {
//	    @SuppressWarnings("unchecked")
//	    ArrayAdapter<Categoria> adapter = (ArrayAdapter<Categoria>) l.getAdapter();
//	    Categoria comment = null;
//	    switch (view.getId()) {
//	    case R.id.add:
//	      String[] comments = new String[] { "Cool", "Very nice", "Hate it" };
//	      int nextInt = new Random().nextInt(3);
//	      // save the new comment to the database
//	      comment = datasourcecat.createCategoria(comments[nextInt],"SISI");
//	      adapter.add(comment);
//	      break;
//	    case R.id.delete:
//	      if (l.getAdapter().getCount() > 0) {
//	        comment = (Categoria) l.getAdapter().getItem(0);
//	        datasourcecat.deleteCategoria(comment);
//	        adapter.remove(comment);
//	      }
//	      break;
//	    }
//	    adapter.notifyDataSetChanged();
//	  }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.category_list, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
//		if (id == R.id.action_settings) {
//			return true;
//		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
//	public static class PlaceholderFragment extends Fragment {
//
//		public PlaceholderFragment() {
//		}
//
//		@Override
//		public View onCreateView(LayoutInflater inflater, ViewGroup container,
//				Bundle savedInstanceState) {
//			View rootView = inflater.inflate(R.layout.fragment_main, container,
//					false);
//			return rootView;
//		}
//	}
	
	public class MyAdapter extends BaseAdapter implements OnClickListener {
	    private Context context;

	    private List<Categoria> categories;

	    public MyAdapter(Context context, List<Categoria> listCategoria) {
	        this.context = context;
	        this.categories = listCategoria;
	    }

	    public int getCount() {
	        return categories.size();
	    }

	    public Object getItem(int position) {
	        return categories.get(position);
	    }

	    public long getItemId(int position) {
	        return position;
	    }

	    public View getView(int position, View convertView, ViewGroup viewGroup) {
	        Categoria entry = categories.get(position);
	        if (convertView == null) {
	            LayoutInflater inflater = (LayoutInflater) context
	                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	            convertView = inflater.inflate(R.layout.categoria, null);
	        }
	        TextView title = (TextView) convertView.findViewById(R.id.title);
	        title.setText(entry.getNome());

	        TextView descrizione = (TextView) convertView.findViewById(R.id.subtitle);
	        descrizione.setText(Html.fromHtml(entry.getDescrizione()));
	       
	        // btnRemove.setId(position);
      
	        return convertView;
	    }

	    @Override
	    public void onClick(View view) {
//	        Phonebook entry = (Phonebook) view.getTag();
//	        listPhonebook.remove(entry);
//	        // listPhonebook.remove(view.getId());
	        notifyDataSetChanged();

	    }

//	    private void showDialog(Phonebook entry) {
//	        // Create and show your dialog
//	        // Depending on the Dialogs button clicks delete it or do nothing
//	    }

	}

}
